﻿Imports DAO
Imports DTO
Imports System.Resources
Imports System.Globalization
Imports System.Reflection

Public Class SachBUS
    Dim ngon As Integer = NgonNgu.ngon
    ' load Thông Tin Sách Lên DataGridView
    Public Function LaySach() As DataTable
        Dim dt As New DataTable
        Dim sach As New SachDAO
        dt = sach.LayDanhSachSach()
        Return dt
    End Function

    Public Sub NhapSach(ByVal sach As SachDTO, ByVal ct As ChiTietSachDTO)
        Dim kt As New QuyDinhDAO
        Dim dt As New DataTable
        dt = kt.LayQuyDinh()
        Dim dr As DataRow = dt.Rows(0)
        Dim nhapmin As Integer = Integer.Parse(dr.Item("SoLuongSachNhapMin"))

        If sach.LaySoLuong < nhapmin Then

            If ngon = 0 Then
                Throw New Exception("Số lượng nhập phải lớn hơn quy định")
            Else
                Throw New Exception("Quantity Add must be greater than regulations")
            End If

        End If
        Dim sach1 As New SachDAO
        sach1.NhapSach(sach, ct)

    End Sub

    Public Sub NhapSachTonTai(ByVal sach As SachDTO, ByVal ct As ChiTietSachDTO)

        Dim chitietDAO As New ChiTietSachDAO
        ct.LayIDSach = sach.LayIdSach

        Dim kt As New QuyDinhDAO
        Dim qd As New DataTable
        qd = kt.LayQuyDinh()

        Dim sl As Integer = SoLuong(sach)
        Dim dt1 As New DataTable
        dt1 = chitietDAO.LayTonCuoi(ct)
        ' ' '' lấy số lượng tồn min
        Dim tonmin As Integer = Int32.Parse(dt1.Rows(0).ItemArray(2).ToString())

        ' ' ' 'lấy qui dinh nhập sach nhỏ nhất
        Dim luongtonmin As Integer = Int32.Parse(qd.Rows(0).ItemArray(2).ToString()) ' '''

        ' 'so luong so nhập phải lon hon qua dinh
        Dim nhapmin As Integer = Int32.Parse(qd.Rows(0).ItemArray(2).ToString())
        If sach.LaySoLuong < nhapmin Then
            If ngon = 0 Then
                Throw New Exception("Số lượng nhập phải lớn hơn quy định")
            Else
                Throw New Exception(" The number entered must be greater than  regulations ")
            End If
        End If

        ' ' ' ' chỉ nhập các đầu sách có lượng tồn ít hơn 300
        If sach.LaySoLuong > tonmin Then
            If ngon = 0 Then
                Throw New Exception("Số lượng đầu Sách vẫn còn nhiều không cần nhập thêm")
            Else
                Throw New Exception("The number of books still no need to add")
            End If
        End If

        Dim sachDAO As New SachDAO
        ' ' ' cập nhật số lượng sách
        sachDAO.ThemDauSachDaTonTai(sach, sl + sach.LaySoLuong)
        Dim chitiet As New ChiTietSachDAO

        chitiet.TonCuoiSachDaTonTai(ct, sach.LaySoLuong + tonmin)

    End Sub

    Public Sub CapNhatSoLuong(ByVal s As SachDTO)
        Dim sach As New SachDAO
        sach.CapNhatSoLuong(s)
    End Sub

    Public Sub XoaSach(ByVal sach As SachDTO)

        Dim s As New SachDAO()
        s.XoaSach(sach)

    End Sub

    Public Sub CapNhat(ByVal sach As SachDTO)

        Dim s As New SachDAO
        s.CapNhatSach(sach)
    End Sub

    Public Function KiemTraSoLuong() As Integer
        Dim dt As New DataTable
        Dim sach As New SachDAO
        dt = sach.KiemTaSoLuong()
        Dim sl As Integer = 0
        Dim dr As DataRow = dt.Rows(0)
        sl = dr.Item("SoLuong")
        Return sl
    End Function

    Public Function SoLuong(ByVal id As SachDTO) As Integer
        Dim dt As New DataTable
        Dim sach As New SachDAO
        dt = sach.laySoLuong(id)
        Dim sl As Integer = 0
        Dim dr As DataRow = dt.Rows(0)
        sl = dr.Item("SoLuong")
        Return sl
    End Function

    Public Function LayGia(ByVal id As SachDTO) As Integer
        Dim dt As New DataTable
        Dim sach As New SachDAO
        dt = sach.LayGia(id)
        Dim sl As Integer = 0
        Dim dr As DataRow = dt.Rows(0)
        sl = dr.Item("Gia")
        Return sl
    End Function

    Public Sub XoaTheloai(ByVal idtheloai As TheLoaiDTO)
        Dim theloai As New TheLoaiDAO
        theloai.XoaTheLoai(idtheloai)
    End Sub

    Public Function LayTenSach(ByVal s As SachDTO) As DataTable
        Dim dt As New DataTable
        Dim sach As New SachDAO
        dt = sach.LayTenSach(s)
        Return dt
    End Function

    ' Public Function ThongKeT()

    Public Function Tim(ByVal s As String) As DataTable
        Dim dt As New DataTable
        Dim ti As New SachDAO
        dt = ti.tim(s)
        Return dt
    End Function

    Public Function TimTheoTen(ByVal s As String) As DataTable
        Dim dt As New DataTable
        Dim ten As New SachDAO
        dt = ten.timtheoten(s)
        Return dt
    End Function

    Public Function TimTheoTacGia_TheLoai(ByVal s As SachDTO) As DataTable
        Dim dt As New DataTable
        Dim sach As New SachDAO
        dt = sach.LaySachTheoTheLoaiVaTacGia(s)
        Return dt
    End Function

    Public Function TimTheoTacGia(ByVal s As SachDTO) As DataTable
        Dim dt As New DataTable
        Dim sach As New SachDAO
        dt = sach.timtheotacgia(s)
        Return dt
    End Function


    Public Function TimTheoTheLoai(ByVal s As SachDTO) As DataTable
        Dim dt As New DataTable
        Dim sach As New SachDAO
        dt = sach.LaySachTheoTheLoai(s)
        Return dt
    End Function

End Class
